package com.kamloo.stg.data.db;

public class AlarmDAO {
	/*
	public static final String 	TABLE_NAME = "Alarmes",
			COL_ID = "id_alarme",
			COL_ID_HORAIRE = "id_horaire_fk",
			COL_TIME = "next_schedule",
			COL_DAYS = "repeating_days";
	
	public static final String CREATE ="CREATE TABLE %s (%s INTEGER PRIMARY KEY AUTOINCREMENT," +
										"%s INTEGER REFERENCES %s(%s) ON UPDATE CASCADE ON DELETE CASCADE," +
										"%s LONG," +
										"%s TEXT);",
								INSERT = "INSERT INTO %s (%s,%s,%s) VALUES (%d,%d,%s);",
								WHERE = "%s = %d",
								SELECT_NODE = "SELECT %s,%s,%s,%s FROM %s INNER JOIN %s on %s = %s INNER JOIN %s on %s = %s WHERE %s.%s = ?;",
								ORDER_ASC = "ORDER BY %s ASC";

	public static final String CREATE_TABLE = 	String.format(CREATE, TABLE_NAME, COL_ID,
											COL_ID_HORAIRE, COL_TIME, COL_DAYS, ScheduleDAO.TABLE_NAME, ScheduleDAO.COL_ID);
	
	public static int add(int id_horaire, long time, String days, SQLiteDatabase db) {
		ContentValues cv = new ContentValues();
		cv.put(COL_ID_HORAIRE, id_horaire);
		cv.put(COL_TIME, time);
		cv.put(COL_DAYS, days);
		
		return (int) db.insert(TABLE_NAME, null, cv);
	}
	
	public static void delete(int id, SQLiteDatabase db) {
		db.delete(TABLE_NAME, String.format(WHERE, COL_ID, id), null);
	}
	
	public static ArrayList<Alarm> selectAll(SQLiteDatabase db){
		ArrayList<Alarm> alarmes = new ArrayList<Alarm>();
		
		Cursor cur = db.query(TABLE_NAME, new String[] {COL_ID, COL_ID_HORAIRE, COL_TIME, COL_DAYS}, null, null,null,null,null);
		
		if (cur != null) {
			while (cur.moveToNext()) {
				alarmes.add(new Alarm(cur.getInt(cur.getColumnIndex(COL_ID)),
									   cur.getInt(cur.getColumnIndex(COL_ID_HORAIRE)),
									   cur.getLong(cur.getColumnIndex(COL_TIME)),
									   AlarmUtility.getDays(cur.getString(cur.getColumnIndex(COL_DAYS)))));
			}
		}
		
		return alarmes;
	}
	
	public static ArrayList<Alarm> selectFromNode(int id_noeud, SQLiteDatabase db) {
		ArrayList<Alarm>alarmes = new ArrayList<Alarm>();
		
		String query = String.format(SELECT_NODE, COL_ID, COL_ID_HORAIRE, COL_TIME, COL_DAYS, TABLE_NAME, 
									ScheduleDAO.TABLE_NAME, COL_ID_HORAIRE, ScheduleDAO.COL_ID, 
									NodeDAO.TABLE_NAME, ScheduleDAO.COL_ID_NOEUD, NodeDAO.COL_ID, 
									NodeDAO.TABLE_NAME, NodeDAO.COL_ID);
		Cursor cur = db.rawQuery(query, new String[] {String.valueOf(id_noeud)});
		
		if (cur != null) {
			while (cur.moveToNext()) {
				alarmes.add(new Alarm(cur.getInt(cur.getColumnIndex(COL_ID)),
									   cur.getInt(cur.getColumnIndex(COL_ID_HORAIRE)),
									   (cur.getLong(cur.getColumnIndex(COL_TIME))),
									   AlarmUtility.getDays(cur.getString(cur.getColumnIndex(COL_DAYS)))));
			}
		}
		
		return alarmes;
	}
	
	public static Alarm select(int id, SQLiteDatabase db) {
		Alarm a = null;
		
		Cursor cur = db.query(TABLE_NAME, new String[] {COL_ID, COL_ID_HORAIRE, COL_TIME, COL_DAYS}, null, null,null,null,null);
		
		if (cur != null && cur.moveToFirst()) {
			String tabarnak = cur.getString(cur.getColumnIndex(COL_DAYS));
			a = new Alarm(cur.getInt(cur.getColumnIndex(COL_ID)),
					   cur.getInt(cur.getColumnIndex(COL_ID_HORAIRE)),
					   cur.getLong(cur.getColumnIndex(COL_TIME)),
					   AlarmUtility.getDays(cur.getString(cur.getColumnIndex(COL_DAYS))));
		}
		return a;
	}
	
	public static void update(Alarm a, SQLiteDatabase db) {
		ContentValues cv = new ContentValues();
		cv.put(COL_ID_HORAIRE, a.getId_horaire());
		cv.put(COL_TIME, a.getTime());
		cv.put(COL_DAYS, AlarmUtility.getDays(a.getRepeatingDays()));
		
		db.update(TABLE_NAME, cv, String.format(WHERE, COL_ID, a.getId()), null);
	}
	
	public static void updateNextSchedule(int id, long time, SQLiteDatabase db) {
		ContentValues cv = new ContentValues();
		cv.put(COL_TIME, time);
		db.update(TABLE_NAME, cv, String.format(WHERE, COL_ID, id), null);
	}
	
	public static Alarm selectNextScheduledAlarm(SQLiteDatabase db) {
		Alarm a = null;
		Cursor cur = db.query(TABLE_NAME, new String[] {COL_ID, COL_ID_HORAIRE, COL_TIME, COL_DAYS}, null, null,null,null,String.format(ORDER_ASC, COL_TIME));
		
		if (cur != null) {
			cur.moveToFirst();
			a = new Alarm(cur.getInt(cur.getColumnIndex(COL_ID)),
								   cur.getInt(cur.getColumnIndex(COL_ID_HORAIRE)),
								   cur.getLong(cur.getColumnIndex(COL_TIME)),
								   AlarmUtility.getDays(cur.getString(cur.getColumnIndex(COL_DAYS))));
		}
		return a;
	}
	public static Boolean alarmExist(int id_horaire, SQLiteDatabase db) {
		Cursor cur = db.query(TABLE_NAME, new String[] { String.format("COUNT(%s)", COL_ID) }, 
				     String.format(WHERE, COL_ID_HORAIRE, id_horaire), null, null, null, null);
		
		return cur != null;
	}
	*/
}
